﻿Imports System.Windows.Forms
Imports System.Drawing.Drawing2D
Imports IWshRuntimeLibrary
Public Class FrmSettings
    Dim FullNames As New Dictionary(Of Integer, String)
    Dim Names As New Dictionary(Of Integer, String)
    Dim Folder As String
    Dim BangbezhFile As String 'The Bangbezh BBJ file in which sound file names are loaded from.

    Dim OldVal As Short
    Dim first As Boolean = True
    Dim Scrolled As Boolean = False
    Dim Shortcut As String = Environment.GetFolderPath(Environment.SpecialFolder.Startup)

    Dim mouseOffset As Point

    Private Sub Me_Closed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.FormClosed
        FrmMain.Show()
    End Sub
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Dim OptionsBBJ As New Ini   'Make a new BBJ file in memory for settings
        Dim Restart As Boolean = False 'Should Bangbezh be restarted?

        OptionsBBJ.Load(Application.StartupPath & "\Data\Options.Bbj") 'Load settings

        'If the user have changed the city, then Bangbezh must be restarted

        If CboCity.SelectedIndex = City Then
        Else
            City = CboCity.SelectedIndex
            Restart = True
        End If

        'Save settings
        OptionsBBJ.SetNumberField("Options", "City", CboCity.SelectedIndex)
        OptionsBBJ.SetNumberField("Options", "Color", SldrColor.Value)
        OptionsBBJ.SetNumberField("Options", "Volume", Volume)
        OptionsBBJ.SetStringField("Options", "Bangbezh", BangbezhFile)
        OptionsBBJ.SetBooleanField("Options", "Startup", ChkStartup.Checked)
        OptionsBBJ.SetBooleanField("Options", "Zikr", ChkZikr.Checked)
        OptionsBBJ.SetBooleanField("Options", "Fix", ChkFix.Checked)
        OptionsBBJ.SetNumberField("Options", "FixMins", CboFix.SelectedIndex)
        'Save the INI file
        OptionsBBJ.Save(Application.StartupPath & "\Data\Options.Bbj")
        FrmMain.TmrAzkar.Enabled = ChkZikr.Checked
        Fix = ChkFix.Checked
        FrmMain.PicFix.BackColor = System.Windows.Forms.ControlPaint.Light(MyColor)
        If Fix Then
            Select Case CboFix.SelectedIndex
                Case 0
                    FixMins = New TimeSpan(0, 5, 0)
                Case 1
                    FixMins = New TimeSpan(0, 10, 0)
                Case 2
                    FixMins = New TimeSpan(0, 15, 0)
            End Select
        Else
            FixMins = New TimeSpan(0, 0, 0)
        End If

        MyColor = Colors(SldrColor.Value)

        'This code block is about the shortcut of bangbezh in startup folder
        If My.Computer.FileSystem.FileExists(Shortcut & "\bangbezh.lnk") Then
            If ChkStartup.Checked = False Then
                My.Computer.FileSystem.DeleteFile(Shortcut & "\bangbezh.lnk")
            End If
        Else
            If ChkStartup.Checked = True Then
                Dim WshShell As New WshShell
                Dim Shrtct As IWshRuntimeLibrary.IWshShortcut = DirectCast(WshShell.CreateShortcut _
                    (Environment.GetFolderPath(Environment.SpecialFolder.Startup) & "\bangbezh.lnk"), IWshRuntimeLibrary.IWshShortcut)

                ' set the shortcut properties
                With Shrtct
                    .TargetPath = Application.ExecutablePath
                    .WindowStyle = 1I
                    .Description = "Bangbezh"
                    .WorkingDirectory = Application.StartupPath
                    ' the next line gets the first Icon from the executing program
                    .IconLocation = Application.ExecutablePath & ", 0"
                    'Add a "-totray" argument, so that it hides into System tray right after openning
                    .Arguments = "-totray"
                    .Save() ' save the shortcut file
                End With
            End If
        End If
        FrmMain.ChangeColor()
        'If Restart was needed, then restart.
        If Restart = True Then
            FrmMain.Close()
            Process.Start(Application.StartupPath & "\Bangbezh.exe")
            End
        End If
        Bangbej.URL = Nothing 'Stop Bangbej from playing (If it was)
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        FrmMain.Show()
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Bangbej.URL = Nothing
        Me.Close()
    End Sub
    Private Sub LnkMore_MouseEnter(sender As Object, e As EventArgs) Handles LnkMore.MouseEnter
        LnkMore.Font = New Font("Segoe UI", 11, FontStyle.Underline)
    End Sub

    Private Sub LnkMore_MouseLeave(sender As Object, e As EventArgs) Handles LnkMore.MouseLeave
        LnkMore.Font = New Font("Segoe UI", 11, FontStyle.Regular)
    End Sub
    Private Sub DlgSettings_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        CboBangbezh.Items.Clear()
        Names.Clear()
        FullNames.Clear()
        Dim BangbezhINI As New Ini
        Dim OptionsBBJ As New Ini 'Make a new INI File for options in memory

        Try

            OptionsBBJ.Load(Application.StartupPath & "\Data\Options.Bbj") 'Load Data into it
            BangbezhFile = OptionsBBJ.GetStringField("Options", "Bangbezh")

            BangbezhINI.Load(Application.StartupPath & "\Data\Dangakan\" & BangbezhFile)
            Folder = BangbezhINI.GetStringField("Bangbezh", "Folder")

            For Each File In My.Computer.FileSystem.GetFiles(Application.StartupPath & "\Data\Dangakan\")
                Dim FileInformation As IO.FileInfo = My.Computer.FileSystem.GetFileInfo(File)
                If FileInformation.Extension.ToUpper = ".BBJ" Then
                    If FullNames.ContainsValue(FileInformation.FullName) Then
                    Else
                        FullNames.Add(FullNames.Count + 1, FileInformation.FullName)
                    End If
                End If
            Next

            For Each Item In FullNames
                Dim INIFile As New Ini
                INIFile.Load(Item.Value)
                If Name.Contains(INIFile.GetStringField("Bangbezh", "Name")) Then
                Else
                    Names.Add(Item.Key, INIFile.GetStringField("Bangbezh", "Name"))
                End If
            Next

            'Add items
            For Each Item In Names
                CboBangbezh.Items.Add(Item.Value)
            Next
            CboBangbezh.Items.Add("زۆرتر...")


            For Each Item In Names
                If Item.Value = BangbezhFile Then
                    CboBangbezh.Text = Names.Item(Item.Key)
                End If
            Next

            'Load settings
            MyColor = Colors(OptionsBBJ.GetNumberField("Options", "Color"))
            CboCity.Text = Cities(City)
            CboBangbezh.Text = BangbezhINI.GetStringField("Bangbezh", "Name")

            Select Case MyColor
                Case Colors(0)
                    SldrColor.Value = 0
                Case Colors(1)
                    SldrColor.Value = 1
                Case Colors(2)
                    SldrColor.Value = 2
                Case Colors(3)
                    SldrColor.Value = 3
                Case Colors(4)
                    SldrColor.Value = 4
                Case Else
                    Exit Select
            End Select

            PicColor1.BackColor = Colors(0)
            PicColor2.BackColor = Colors(1)
            PicColor3.BackColor = Colors(2)
            PicColor4.BackColor = Colors(3)
            PicColor5.BackColor = Colors(4)

            Volume = OptionsBBJ.GetNumberField("Options", "Volume")
            SldrVolume.Value = Volume
            LblVolume.Text = HinduNumeric(SldrVolume.Value)

            Button_Colors()

            ChkStartup.Checked = OptionsBBJ.GetBooleanField("Options", "Startup")
            ChkZikr.Checked = OptionsBBJ.GetBooleanField("Options", "Zikr")
            ChkFix.Checked = OptionsBBJ.GetBooleanField("Options", "Fix")
            CboFix.SelectedIndex = OptionsBBJ.GetNumberField("Options", "FixMins")
            TickBox1.Checked = ChkStartup.Checked
            TickBox2.Checked = ChkZikr.Checked
            TickBox3.Checked = ChkFix.Checked
            CboFix.Enabled = ChkFix.Checked
            TickBox1.TickColor = MyColor
            TickBox2.TickColor = MyColor
            TickBox3.TickColor = MyColor
            PnlTitle.BackColor = MyColor
            TBtnGeneral.CheckedColor = MyColor
            TBtnAppearance.CheckedColor = MyColor
            TBtnVoice.CheckedColor = MyColor
            BtnClose.FlatAppearance.MouseOverBackColor = ControlPaint.Light(MyColor)
            first = False
            TBtnGeneral.Checked = True
            ChangeFocus(0)
            'Check if Bangbezh's link is in startup
            ChkStartup.Checked = My.Computer.FileSystem.FileExists(Shortcut + "\Bangbezh.lnk")

        Catch ex As InvalidOperationException
            If ex.Message.Contains(GetStartupPath() & "\Data\Options.bbj") Then
                WriteOptions()
                MsgBox("ڕێكخستنەكان داندرانەوە.", vbInformation, "دانانەوەی ڕێكخستنەكان")
                Process.Start(Application.StartupPath & "\Bangbezh.exe")
                FrmMain.Close()
                End
            Else
                Throw ex
            End If

        End Try
    End Sub

    Private Sub SldrVolume_ValueChanged(sender As Object, e As EventArgs) Handles SldrVolume.ValueChanged
        LblVolume.Text = HinduNumeric(SldrVolume.Value)
        Volume = SldrVolume.Value
    End Sub

    Private Sub BtnTest_Click(sender As Object, e As EventArgs) Handles BtnTest.Click
        Bangbej.settings.volume = Volume
        Bangbej.URL = (Application.StartupPath & "\Data\Dangakan\" & Folder & "\2.mp3")
    End Sub

    Private Sub CboBangbezh_GotFocus(sender As Object, e As EventArgs) Handles CboBangbezh.GotFocus
        'Set the value of OldVal to currents value, Old val is used when user clicks on "زۆرتر..." to change
        'it back to current Bangbezh and open Bangbezh's Download page.
        OldVal = CboBangbezh.SelectedIndex
    End Sub
    Private Sub CboBangbezh_MouseHover(sender As Object, e As EventArgs) Handles CboBangbezh.MouseHover
        ToolTipExplain.Show(CboBangbezh.Text, CboBangbezh)
    End Sub

    Private Sub CboBangbezh_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CboBangbezh.SelectedIndexChanged
        'If the user wanted more sounds then send him to Bangbezh's download page.
        If CboBangbezh.Text = "زۆرتر..." Then
            CboBangbezh.SelectedIndex = OldVal
            Process.Start("http://bangbezh.com/p/dangakan.html")
        End If
        'Change the value of Bangbezhfile
        For Each Item In Names
            If Item.Value = CboBangbezh.Text Then
                Dim Fileinfor As IO.FileInfo = My.Computer.FileSystem.GetFileInfo(FullNames.Item(Item.Key))
                BangbezhFile = Fileinfor.Name
            End If
        Next
        'Load information about the new bangbezh so that the user can test his voice.
        Dim BangbezhINI As New Ini
        If My.Computer.FileSystem.FileExists(Application.StartupPath & "\Data\Dangakan\" & BangbezhFile) Then
            BangbezhINI.Load(Application.StartupPath & "\Data\Dangakan\" & BangbezhFile)
            Folder = BangbezhINI.GetStringField("Bangbezh", "Folder")
        Else
            WriteOptions()
        End If

    End Sub

    Sub ChangeFocus(ByVal Focus As Short)
        TabControl1.SelectedIndex = Focus
        Me.Text = String.Format("ڕێكخستنەكان - {0}", TabControl1.SelectedTab.Text)
        LblTitle.Text = Me.Text
    End Sub
    Sub Hver(_Lbl As Label)
        _Lbl.ForeColor = ControlPaint.Light(MyColor)
    End Sub

  

    Private Sub ChkZikr_CheckedChanged(sender As Object, e As EventArgs) Handles ChkZikr.CheckedChanged
        TickBox2.Checked = ChkZikr.Checked
    End Sub
    Private Sub PicColor5_Click(sender As Object, e As EventArgs) Handles PicColor5.Click
        SldrColor.Value = 4
    End Sub
    Private Sub PicColor4_Click(sender As Object, e As EventArgs) Handles PicColor4.Click
        SldrColor.Value = 3
    End Sub
    Private Sub PicColor3_Click(sender As Object, e As EventArgs) Handles PicColor3.Click
        SldrColor.Value = 2
    End Sub
    Private Sub PicColor2_Click(sender As Object, e As EventArgs) Handles PicColor2.Click
        SldrColor.Value = 1
    End Sub
    Private Sub PicColor1_Click(sender As Object, e As EventArgs) Handles PicColor1.Click
        SldrColor.Value = 0
    End Sub

    Private Sub TrkBrPr_MouseDown(sender As Object, e As MouseEventArgs) Handles SldrColor.MouseDown
        SldrVolume.Cursor = Cursors.NoMoveHoriz
    End Sub

    Private Sub SldrVolume_MouseDown(sender As Object, e As MouseEventArgs) Handles SldrVolume.MouseDown
        SldrVolume.Cursor = Cursors.NoMoveHoriz
    End Sub

    Private Sub SldrVolume_MouseUp(sender As Object, e As MouseEventArgs) Handles SldrVolume.MouseUp
        SldrVolume.Cursor = Cursors.Default
    End Sub

    Private Sub TrkBrPr_Scroll(sender As Object, e As EventArgs) Handles SldrColor.Scroll

    End Sub

    Private Sub TrkBrPr_MouseUp(sender As Object, e As MouseEventArgs) Handles SldrColor.MouseUp
        SldrVolume.Cursor = Cursors.Default
    End Sub

    Private Sub ChbxStartup_CheckedChanged(sender As Object, e As EventArgs) Handles ChkStartup.CheckedChanged
        TickBox1.Checked = ChkStartup.Checked
    End Sub

    Private Sub TickBox2_CheckedChanged(sender As Object, e As EventArgs) Handles TickBox2.CheckedChanged
        ChkZikr.Checked = TickBox2.Checked
    End Sub

    Private Sub TickBox1_CheckedChanged(sender As Object, e As EventArgs) Handles TickBox1.CheckedChanged
        ChkStartup.Checked = TickBox1.Checked
    End Sub

    Private Sub ChkStartup_MouseHover(sender As Object, e As EventArgs) Handles ChkStartup.MouseHover
        TickBox1.Hover = True
    End Sub

    Private Sub ChkStartup_MouseLeave(sender As Object, e As EventArgs) Handles ChkStartup.MouseLeave
        TickBox1.Hover = False
    End Sub

    Private Sub ChkStartup_MouseEnter(sender As Object, e As EventArgs) Handles ChkStartup.MouseEnter
        TickBox1.Hover = True
    End Sub

    Private Sub ChkZikr_MouseEnter(sender As Object, e As EventArgs) Handles ChkZikr.MouseEnter
        TickBox2.Hover = True
    End Sub

    Private Sub ChkZikr_MouseHover(sender As Object, e As EventArgs) Handles ChkZikr.MouseHover
        TickBox2.Hover = True
    End Sub

    Private Sub ChkZikr_MouseLeave(sender As Object, e As EventArgs) Handles ChkZikr.MouseLeave
        TickBox2.Hover = False
    End Sub

    Private Sub ChkFix_CheckedChanged(sender As Object, e As EventArgs) Handles ChkFix.CheckedChanged
        TickBox3.Checked = ChkFix.Checked
        CboFix.Enabled = ChkFix.Checked
    End Sub

    Private Sub ChkFix_MouseEnter(sender As Object, e As EventArgs) Handles ChkFix.MouseEnter
        TickBox3.Hover = True
    End Sub

    Private Sub ChkFix_MouseHover(sender As Object, e As EventArgs) Handles ChkFix.MouseHover
        TickBox3.Hover = True
    End Sub

    Private Sub ChkFix_MouseLeave(sender As Object, e As EventArgs) Handles ChkFix.MouseLeave
        TickBox3.Hover = False
    End Sub
    Private Sub TickBox3_CheckedChanged(sender As Object, e As EventArgs) Handles TickBox3.CheckedChanged
        ChkFix.Checked = TickBox3.Checked
    End Sub
    Private Sub Button_Reset_Click(sender As Object, e As EventArgs) Handles Reset_Button.Click
        SldrColor.Value = 0
        SldrVolume.Value = 65
        ChkStartup.Checked = True
        ChkZikr.Checked = True
        ChkFix.Checked = False
    End Sub

    Private Sub BtnHlp1_Click(sender As Object, e As EventArgs) Handles BtnHlp1.Click
        Dim _content As String
        _content = "ئەو پارێزگایە دیاری بکە کە تێیدا نیشتەجێی،" _
            & " بۆ ئەوەی بانگبێژ بە پێی کاتی بانگی " _
            & "پارێزگاکەت بانگبدات."

        showHelp(_content, 1)
    End Sub

    Private Sub BtnHlp2_Click(sender As Object, e As EventArgs) Handles BtnHlp2.Click
        Dim _content As String
        _content = "ئەگەر بتەوێ بانگبێژ لە کاتی نوێژەکاندا " _
            & "بانگبدات، ئەوا دەبێ بانگبێژ لەگەڵ پێبوونی " _
            & "کۆمپیوتەرەکەت دەستپێبکات."

        showHelp(_content, 1)
    End Sub

    Private Sub BtnHlp3_Click(sender As Object, e As EventArgs) Handles BtnHlp3.Click
        Dim _content As String
        _content = "ئایا دەتەوێت بانگبێژ هەموو ٧ خولەک " _
            & "جارێک یادی خودات بەبیر بهێنێتەوە؟"

        showHelp(_content, 1)
    End Sub

    Private Sub BtnHlp4_Click(sender As Object, e As EventArgs) Handles BtnHlp4.Click
        Dim _content As String
        _content = "ئەگەر دەتەوێت نوێژکانت لە مزگەوت بکەیت، " _
            & " ئەوا دەتوانی کاتی بانگەکان بهێنیتە پێش " _
            & "بۆ ئەوەی بە نوێژی بە کۆمەڵ ڕابگەیت."

        showHelp(_content, 1)
    End Sub
    Sub Button_Colors()
        OK_Button.FlatAppearance.MouseOverBackColor = MyColor
        Cancel_Button.FlatAppearance.MouseOverBackColor = MyColor
        Reset_Button.FlatAppearance.MouseOverBackColor = MyColor
        BtnHlp1.FlatAppearance.MouseOverBackColor = MyColor
        BtnHlp2.FlatAppearance.MouseOverBackColor = MyColor
        BtnHlp3.FlatAppearance.MouseOverBackColor = MyColor
        BtnHlp4.FlatAppearance.MouseOverBackColor = MyColor
        BtnHlp5.FlatAppearance.MouseOverBackColor = MyColor
        BtnHlp6.FlatAppearance.MouseOverBackColor = MyColor
        BtnHlp7.FlatAppearance.MouseOverBackColor = MyColor
        BtnTest.FlatAppearance.MouseOverBackColor = MyColor

    End Sub

    Private Sub BtnHlp5_Click(sender As Object, e As EventArgs) Handles BtnHlp5.Click
        Dim _content As String
        _content = "ئەو ڕەنگە هەڵبژێرە کە حەزی پێدەکەیت."

        showHelp(_content, 0)
    End Sub

    Private Sub BtnHlp6_Click(sender As Object, e As EventArgs) Handles BtnHlp6.Click
        Dim _content As String
        _content = "ئەو بانگبێژە هەڵبژێرە کە حەز بە دەنگی دەکەیت، بۆ ئەوەی بانگبێژ بەو دەنگە " _
           & "بانگبدات.وە بۆ تاقی کردنەوەی ئەو دەنگەی کە هەڵتبژاردووە، کلیک لەسەر دوگمەی " _
           & "تاقیکردنەوە بکە، و دەشتوانی دەنگی بانگبێژی دیکە لە وێبسایتی بانگبێژ داببەزێنی بە " _
           & "کلیک کردن لەسەر ""دەنگی زۆرتر داببەزێنە""."

        showHelp(_content, 2)
    End Sub

    Private Sub BtnHlp7_Click(sender As Object, e As EventArgs) Handles BtnHlp7.Click
        Dim _content As String
        _content = "ئاستی دەنگی بانگبێژ لەکاتی بانگداندا دیاری بکە."

        showHelp(_content, 0)
    End Sub

    Private Sub PictTitle_MouseDown(sender As Object, e As MouseEventArgs) Handles PnlTitle.MouseDown, LblTitle.MouseDown
        mouseOffset = New Point(-e.X, -e.Y)
    End Sub

    Private Sub PictTitle_MouseMove(sender As Object, e As MouseEventArgs) Handles PnlTitle.MouseMove, LblTitle.MouseMove
        If e.Button = MouseButtons.Left Then
            Dim mousePos = Control.MousePosition
            mousePos.Offset((mouseOffset.X), (mouseOffset.Y))
            Location = mousePos
        End If
    End Sub
    Private Sub BtnClose_Click(sender As Object, e As EventArgs) Handles BtnClose.Click
        Cancel_Button.PerformClick()
    End Sub

    Sub showHelp(ByVal _Text As String, ByVal _Size As Short)
        Select Case _Size
            Case 0
                DlgHlp.Height = 55
                DlgHlp.lblContent.TextAlign = ContentAlignment.MiddleCenter
            Case 1
                DlgHlp.Height = 80
                DlgHlp.lblContent.TextAlign = ContentAlignment.MiddleLeft
            Case Else
                DlgHlp.Height = 130
                DlgHlp.lblContent.TextAlign = ContentAlignment.MiddleLeft
        End Select
        DlgHlp.lblContent.Text = _Text
        DlgHlp.ShowDialog()
    End Sub

   
    Private Sub TBtnGeneral_Click(sender As Object, e As EventArgs) Handles TBtnGeneral.Click
        ChangeFocus(0)
    End Sub

    Private Sub TBtnAppearance_Click(sender As Object, e As EventArgs) Handles TBtnAppearance.Click
        ChangeFocus(1)
    End Sub

    Private Sub TBtnVoice_Click(sender As Object, e As EventArgs) Handles TBtnVoice.Click
        ChangeFocus(2)
    End Sub

    Private Sub LnkMore_Click(sender As Object, e As EventArgs) Handles LnkMore.Click
        Process.Start("http://bangbezh.com/p/dangakan.html")
    End Sub
End Class